package com.hippo.core.ui.components

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import kotlin.math.atan2

@Composable
fun ControlManu(modifier: Modifier = Modifier) {

    Box(
        modifier = Modifier
            .size(200.dp)
            .pointerInput(Unit) {
                detectTapGestures(onTap = { offset ->
                    handleTap(offset)
                })
            }
    ) {
        Canvas(modifier = Modifier.size(400.dp)) {
            val radius = size.minDimension / 2
            val center = Offset(size.width / 2, size.height / 2)
            val innerRadius = radius * 0.4f  // 内圆半径
            val outerRadius = radius * 0.8f  // 外圆半径
            val arcWidth = 40.dp.toPx()  // 弧线宽度
            val gap = 8.dp.toPx()  // 间隔
            val totalAngle = 360f
            val partAngle = (totalAngle - 4 * gap) / 4  // 每个部分的角度

            // 绘制中间的圆形
            drawCircle(
                color = Color.Gray,
                radius = innerRadius
            )

            // 绘制四个部分
            drawArc(
                color = Color.Blue,
                startAngle = 0f,
                sweepAngle = partAngle +  gap/10,
                useCenter = false,
                topLeft = Offset(center.x - outerRadius, center.y - outerRadius),
                size = Size(outerRadius * 2, outerRadius * 2),
                style = Stroke(width = arcWidth)
            )
            drawArc(
                color = Color.Green,
                startAngle = partAngle +  gap/2,
                sweepAngle = partAngle + gap,
                useCenter = false,
                topLeft = Offset(center.x - outerRadius, center.y - outerRadius),
                size = Size(outerRadius * 2, outerRadius * 2),
                style = Stroke(width = arcWidth)
            )
            drawArc(
                color = Color.Yellow,
                startAngle = 2 * partAngle + 2 * gap,
                sweepAngle = partAngle ,
                useCenter = false,
                topLeft = Offset(center.x - outerRadius, center.y - outerRadius),
                size = Size(outerRadius * 2, outerRadius * 2),
                style = Stroke(width = arcWidth)
            )

            drawArc(
                color = Color.Red,
                startAngle = 3 * partAngle + 3 * gap,
                sweepAngle = partAngle,
                useCenter = false,
                topLeft = Offset(center.x - outerRadius, center.y - outerRadius),
                size = Size(outerRadius * 2, outerRadius * 2),
                style = Stroke(width = arcWidth)
            )
        }
    }

}
private fun handleTap(offset: Offset) {
    val radius = 100f
    val center = Offset(100f, 100f)

    val angle = Math.toDegrees(atan2(offset.y - center.y, offset.x - center.x).toDouble())
    val normalizedAngle = (angle + 360) % 360

//    when(normalizedAngle) {
//        in 0..90 -> println("Top Right Clicked")
//        in 90..180 -> println("Top Left Clicked")
//        in 180..270 -> println("Bottom Left Clicked")
//        in 270..360 -> println("Bottom Right Clicked")
//    }
}



@Composable
fun ParallelArcsExample() {
    Box(
        modifier = Modifier.size(200.dp)
    ) {
        Canvas(modifier = Modifier.size(200.dp)) {
            val center = Offset(size.width / 2, size.height / 2)
            val radius = size.minDimension / 3
            val arcWidth = 10.dp.toPx()
            val totalAngle = 360f
            val numArcs = 4
            val gap = 10f  // 间隔角度
            val sweepAngle = (totalAngle - (numArcs - 1) * gap) / numArcs  // 每个扇形的扫过角度

            // 绘制四个平行的扇形
            for (i in 0 until numArcs) {
                val startAngle = i * (sweepAngle + gap)
                drawArc(
                    color = when (i) {
                        0 -> Color.Blue
                        1 -> Color.Green
                        2 -> Color.Yellow
                        3 -> Color.Red
                        else -> Color.Black
                    },
                    startAngle = startAngle,
                    sweepAngle = sweepAngle,
                    useCenter = false,
                    topLeft = Offset(center.x - radius, center.y - radius),
                    size = Size(radius * 2, radius * 2),
                    style = Stroke(width = arcWidth)
                )
            }
        }
    }
}




@Preview
@Composable
private fun ControlManuPreview() {
//    ControlManu()
    ParallelArcsExample()
}